using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class PictureManager
	{
		#region CONSTRUCTORS
		static PictureManager()
		{
			_data = new PictureData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static PictureData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string pictureName,  int categoryID,  int imgLength,  byte[] pictureURL,  string pictureData,  string location,  long sMSCount,  string createdBy,  DateTime createdDate,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[10];
				values[0] = Util.ReplaceNullTo(pictureName, DBNull.Value);
				values[1] = categoryID;
				values[2] = Util.ReplaceNullTo(imgLength, DBNull.Value);
				values[3] = Util.ReplaceNullTo(pictureURL, DBNull.Value);
				values[4] = Util.ReplaceNullTo(pictureData, DBNull.Value);
				values[5] = location;
				values[6] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[7] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[8] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[9] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(PictureInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[10];
				values[0] = Util.ReplaceNullTo(info.PictureName, DBNull.Value);
				values[1] = info.CategoryID;
				values[2] = Util.ReplaceNullTo(info.ImgLength, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.PictureURL, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.PictureData, DBNull.Value);
				values[5] = info.Location;
				values[6] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( long pictureID,  string pictureName,  int categoryID,  int imgLength,  byte[] pictureURL,  string pictureData,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[8];
				values[0] = pictureID;
				values[1] = Util.ReplaceNullTo(pictureName, DBNull.Value);
				values[2] = categoryID;
				values[3] = Util.ReplaceNullTo(imgLength, DBNull.Value);
				values[4] = Util.ReplaceNullTo(pictureURL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(pictureData, DBNull.Value);
				values[6] = location;
				values[7] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( PictureInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[8];
				values[0] = info.PictureID;
				values[1] = Util.ReplaceNullTo(info.PictureName, DBNull.Value);
				values[2] = info.CategoryID;
				values[3] = Util.ReplaceNullTo(info.ImgLength, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.PictureURL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.PictureData, DBNull.Value);
				values[6] = info.Location;
				values[7] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long pictureID)
		{
			_error = "";
			try
			{
				object[] values = { pictureID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( PictureInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.PictureID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static PictureInfo Select( long pictureID)
		{
			_error = "";
			try
			{
				object[] values = { pictureID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				PictureInfo info = new PictureInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return PictureManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return PictureManager.Search( "", "", pageIndex, pageSize);
		}

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                return _data.SearchBy(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Approve(int picId, byte approveValue)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = picId;
                values[1] = approveValue;
                return _data.Approve(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

		#endregion
		
	}
}